function GetCaptcha(){
    $.ajax({
        url: "/reader/sendEmail",
        type: "POST",
        data : {readMail:$("#inputEmail").val()},
        success:function(data){
            if(data){
                alert("获取验证码成功,请前往邮箱查看");
            }else{
                alert("获取验证码失败");
            }
        },
        error:function(){
            alert("请求失败");
        }
    })
}

window.onload = () => {
    var btn = document.getElementById("getCaptcha");
    btn.addEventListener('click', function () {
        alert("邮箱验证码已发,请注意查收");
        var time = 59;
        btn.disabled = true;
        var timer = setInterval(function () {
            if (time === 0) {
                clearInterval(timer);
                btn.disabled = false;
                btn.innerHTML = '发送验证码';
            } else {
                btn.innerHTML = `${time}秒后重新获取`;
                time--;
            }
        }, 1000);
    });
}

$('#reset-button').click(function (event) {
    event.preventDefault();
    var readId = $('#inputreadId').val();
    var readMail = $('#inputEmail').val();
    var captcha = $('#inputCaptcha').val();
    var newPassword = $('#inputNewPass').val();
    var againPassword = $('#inputAgain').val();
    $.ajax({
        type: 'POST',
        url: '/reader/forgetPass',
        data: { readId: readId,readMail: readMail,captcha: captcha, newPass: newPassword, againNewPass: againPassword},
        success: function (res) {
            if (res === 'success') {
                alert('重置密码成功');
                window.location.href = "./loginHtml";
            } else if (res === 'passError') {
                alert('两次密码不一致，请重新输入');
            } else if (res === 'captchaError') {
                alert('验证码错误，请重新获取');
            } else if (res === 'notExist') {
                alert('用户不存在');
            } else if (res === 'mailError'){
                alert('邮箱错误，与注册邮箱不一致');
            } else {
                alert('未知的返回结果，请联系管理员！');
            }
        },
        error: function () {
            alert('请求发生错误，请稍后再试！');
        }
    });
});

function checkExist(){
    $.ajax({
        url: '/reader/checkId',
        type:'POST',
        data:{readId:$('#inputreadId').val()},
        success:function(data){
            if(data === "notExist"){
                alert("账号不存在");
            }
        },
        error:function(){
            alert("请求失败");
        }
    })
}